const galleryInfo = require('./../client/data/galleryInfo')

function ratingParser (d) {
  var num1
  var num2
  var rate = 5
  var reg = new RegExp('\\d+px', 'g')
  if (reg.test(d)) {
    num1 = parseInt(d.match(reg)[0].replace('px', ''))
    num2 = parseInt(d.match(reg)[1].replace('px', ''))
  } else {
    return null
  }
  rate = rate - num1 / 16
  if (num2 === 21) {
    rate--
    return (rate + '.5')
  } else {
    return rate.toString()
  }
}

function generateSLang (title) {
  var simpleLanguage = null
  for (let i = 0; i < galleryInfo.S_LANGS.length; i++) {
    if (new RegExp(galleryInfo.REGS[i], 'gi').test(title)) {
      simpleLanguage = galleryInfo.S_LANGS[i]
    }
  }
  return simpleLanguage
}

function loadImg (b) {
  if (b !== undefined) {
    var a = b.split('~', 4)
    if (a.length === 4) {
      if (a[0] === 'init') {
        return ['http://' + a[1] + '/' + a[2], a[3]]
      } else {
        if (a[0] === 'inits') {
          return ['https://' + a[1] + '/' + a[2], a[3]]
        }
      }
    }
  }
}

module.exports = {
  ratingParser: ratingParser,
  generateSLang: generateSLang,
  loadImg: loadImg
}
